/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;


import MODEL.*;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rodrigo
 */
public class DaoVenda extends Banco{
    
    public DaoVenda(){
        connection=Conecta();
    }
    
    public void insereVenda(Venda venda){  
        try {
            String SQL = "INSERT INTO venda VALUES (default,?,NOW(),default)";
            PreparedStatement stmt = connection.prepareStatement(SQL);
            stmt.setInt(1, venda.getMesa().getCodigo());

            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DaoVenda.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            Fecha();
        }
       
    }
    
    public List<Venda> getVendas(){
        List<Venda> vendas = new ArrayList<Venda>();
        try {    
            Statement stm = connection.createStatement();
             // Vamos executar o seguinte comando SQL :  
            String SQL = "SELECT * FROM venda";  
            ResultSet rs = stm.executeQuery(SQL);  

            while(rs.next())  
            {  
               Venda v = new Venda();
                // Os métodos getXXX recuperam os dados de acordo com o tipo SQL do dado:
               v.setCodigo(rs.getInt("cod_venda"));
               v.setData_paga(rs.getDate("data_paga"));
               v.setData_venda(rs.getDate("data_venda"));
               DaoMesa mesa = new DaoMesa();     
               Mesa m = mesa.getMesa(rs.getInt("cod_mesa"));
               v.setMesa(m);
               vendas.add(v);   
            } 
            
        } catch (Exception ex) {
            System.out.println(ex);
        }finally{
            Fecha();
        }
       
        return vendas;
    }
    
        public Mesa getMesas (int cod_venda){
            Mesa m = new Mesa();
            try{
                 Statement stm = connection.createStatement();
                String SQL = "SELECT * FROM venda WHERE data_paga IS NULL AND cod_venda="+cod_venda; 
                ResultSet rs = stm.executeQuery(SQL);
                while(rs.next()){
                    DaoMesa me = new DaoMesa();     
                    m = me.getMesa(rs.getInt("cod_mesa"));
                }
                //return m;
            }catch(Exception e){
                
            }finally{
                Fecha();
            }
        return m;          
}
        
    public Venda getVendas(int cod_mesa){
        Venda v = new Venda();
        try {    
            Statement stm = connection.createStatement();
            String SQL = "SELECT * FROM venda WHERE cod_mesa="+cod_mesa+" AND data_paga IS NULL";  
            ResultSet rs = stm.executeQuery(SQL);  

            while(rs.next())  
            {  
               v.setCodigo(rs.getInt("cod_venda"));
               v.setData_paga(rs.getDate("data_paga"));
               v.setData_venda(rs.getDate("data_venda"));
               Mesa m = new Mesa();
               m.setCodigo(rs.getInt("cod_mesa"));
               v.setMesa(m);
            } 
            
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return v;
}

    
    public boolean finalizaVenda(int cod_mesa){
        try {    
            String SQL = "UPDATE venda SET data_paga=NOW() WHERE cod_mesa="+cod_mesa;
            PreparedStatement stmt = connection.prepareStatement(SQL);  
            
            stmt.execute();  
            stmt.close();  
            
        } catch (Exception ex) {
            System.out.println(ex);
            return false;
        }finally{
            Fecha();
            return true;
        }
    }
}
